package com.ipan.kits.timer;

import java.text.ParseException;
import java.util.Date;

import com.ipan.kits.base.ExceptionUtil;
import com.ipan.kits.time.DateFormatUtil;

/**
 * 默认时间段验证器；
 * 
 * @author iPan
 * @version 2013-07-16
 */
public class DefaultValidater implements TaskValidateable {
	public static final String DATE_PATTERN = "HH:mm:ss";
	public static final int DAY_TIME = 1000 * 60 * 60 * 24;
	public static Date DEFAULT_START_TIME = null;
	public static Date DEFAULT_END_TIME = null;

	/** 开始时间 */
	protected Date startTime = DEFAULT_START_TIME;

	/** 结束时间 */
	protected Date endTime = DEFAULT_END_TIME;
	
	static {
		try {
			DEFAULT_START_TIME = DateFormatUtil.parseDate(DATE_PATTERN, "1:00:00");
			DEFAULT_END_TIME = DateFormatUtil.parseDate(DATE_PATTERN, "7:00:00");
		} catch (Exception e) {
			ExceptionUtil.unchecked(e);
		}
	}
	
	public DefaultValidater() {
	}
	public DefaultValidater(String startHms, String endHms) {
		try {
			this.startTime = DateFormatUtil.parseDate(DATE_PATTERN, startHms);
			this.endTime = DateFormatUtil.parseDate(DATE_PATTERN, endHms);
		} catch (ParseException e) {
			throw new RuntimeException("时间格式HH:mm:ss不正确", e);
		}
	}

	public boolean validate() {
		long curTime = getHhmmss();
		boolean result = (curTime >= startTime.getTime() && curTime <= endTime.getTime());
		return result;
	}

	private long getHhmmss() {
		Date curTime = null;
		try {
			String hms = DateFormatUtil.formatDate(DATE_PATTERN, new Date());
			curTime = DateFormatUtil.parseDate(DATE_PATTERN, hms);
		} catch (Exception e) {
			ExceptionUtil.unchecked(e);
		}
		return curTime.getTime();
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

}
